package com.gcxy.core;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2024-2024, BUG闭退！
 * Author: Huang先森
 * Date: 2024/7/22 13:03
 * FileName: PageVO
 * Description:
 */
@Data
@TableName("PageVO")
@ApiModel(value = "分页基础类", description = "分页基础类")
public class PageVO<T> implements Serializable {
    @ApiModelProperty(value = "页数 默认为0")

    private Long current = 1L;

    @ApiModelProperty(value = "条数 默认为10")

    private Long size = 3L;

    @ApiModelProperty(value = "总条数")
    private Long total;

    @ApiModelProperty(value = "总页数")
    private Long pages;

    @ApiModelProperty(value = "统一数据")
    private List<T> records;

    public PageVO() {
    }

    public PageVO(Long current, Long size) {
        this.current = current;
        this.size = size;
    }

    public static <T, F> PageVO<F> getPageVo(Page<T> page, Class<F> fClass) {
        PageVO<F> pageVO = new PageVO<>();
        pageVO.setCurrent(page.getCurrent());
        pageVO.setSize(page.getSize());
        pageVO.setTotal(page.getTotal());
        pageVO.setPages(page.getPages());
        List<F> collect = page.getRecords().stream().map(i -> {
            try {
                F f = fClass.newInstance();
                BeanUtils.copyProperties(i, f);
                return f;
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
        pageVO.setRecords(collect);

        return pageVO;
    }
}
